package com.lib;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import androidx.core.content.ContextCompat;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 *    author : Liuxiongbiao
 *    e-mail : liuxiongbiao@cacfintech.com
 *    date   : 2025/2/1014:31
 *    desc   :
 *    version: 1.0
 */
public class FileUtils {
    public static String TAG="FileUtils";
    /**
     * 检查是否有外部存储的写权限
     */
    public static boolean hasWritePermission(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == android.content.pm.PackageManager.PERMISSION_GRANTED;
        }
        return true; // Android 6.0 以下版本默认有权限
    }

    /**
     * 获取应用的私有存储目录（无需权限）
     */
    public static File getPrivateStorageDir(Context context, String dirName) {
        File file = new File(context.getFilesDir(), dirName);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    /**
     * 获取外部存储的公共目录（需要权限）
     */
    public static File getPublicStorageDir(String dirName) {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), dirName);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    /**
     * 获取外部存储的应用专属目录（Android 10+ 推荐使用）
     */
    public static File getExternalStorageDir(Context context, String dirName) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // Android 10+ 使用 Scoped Storage
            return new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), dirName);
        } else {
            // Android 10 以下版本使用传统方式
            return new File(Environment.getExternalStorageDirectory(), dirName);
        }
    }

    /**
     * 写入文件到私有存储
     */
    public static boolean writeToPrivateStorage(Context context, String fileName, String content) {
        File file = new File(getPrivateStorageDir(context, "MyApp"), fileName);
        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(content.getBytes());
            return true;
        } catch (IOException e) {
            Log.e(TAG, "写入文件失败: " + e.getMessage());
            return false;
        }
    }

    /**
     * 从私有存储读取文件
     */
    public static String readFromPrivateStorage(Context context, String fileName) {
        File file = new File(getPrivateStorageDir(context, "MyApp"), fileName);
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] buffer = new byte[(int) file.length()];
            fis.read(buffer);
            return new String(buffer);
        } catch (IOException e) {
            Log.e(TAG, "读取文件失败: " + e.getMessage());
            return null;
        }
    }

    /**
     * 写入文件到外部存储（需要权限）
     */
    public static boolean writeToExternalStorage(Context context, String fileName, String content) {
        if (!hasWritePermission(context)) {
            Log.e(TAG, "无写权限");
            return false;
        }
        File file = new File(getExternalStorageDir(context, "MyApp"), fileName);
        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(content.getBytes());
            return true;
        } catch (IOException e) {
            Log.e(TAG, "写入文件失败: " + e.getMessage());
            return false;
        }
    }

    /**
     * 从外部存储读取文件（需要权限）
     */
    public static String readFromExternalStorage(Context context, String fileName) {
        if (!hasWritePermission(context)) {
            Log.e(TAG, "无读权限");
            return null;
        }
        File file = new File(getExternalStorageDir(context, "MyApp"), fileName);
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] buffer = new byte[(int) file.length()];
            fis.read(buffer);
            return new String(buffer);
        } catch (IOException e) {
            Log.e(TAG, "读取文件失败: " + e.getMessage());
            return null;
        }
    }
}
